/*
 * @Author: xiaosihan
 * @Date: 2024-08-01 15:30:19
 * @Last Modified by: xiaosihanosihan
 * @Last Modified time: 2025-10-02 14:27:02
 */

import { Sequelize, DataTypes } from 'sequelize';
import utils from "./utils";
// 数据库对象
class Database {

    constructor() {
        console.log(process.env.DB_USER);
        console.log(process.env.DB_NAME);
        console.log(process.env.DB_PASSWORD);
        console.log(process.env.DB_HOST);
        console.log(process.env.DB_PORT);
    }

    sequelize = new Sequelize(
        process.env.DB_NAME || "",
        process.env.DB_USER || "",
        process.env.DB_PASSWORD || "",
        {
            // 主机地址  
            host: process.env.DB_HOST || "",
            dialect: 'mysql',
            port: Number(process.env.DB_PORT) || 3306,
            logging: utils.isProd ? false : console.log,
        });

    //创建一个keys表对象
    keys = this.sequelize.define('keys', {
        id: {
            autoIncrement: true,
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true
        },
        ting_png_key: {
            type: DataTypes.STRING(100),
            allowNull: true,
            comment: "ting_png的key"
        },
        compressionCount: {
            type: DataTypes.INTEGER,
            allowNull: true,
            comment: "每月500的免费额度,当前已使用"
        },
        residue: {
            type: DataTypes.INTEGER,
            allowNull: true,
            comment: "剩余免费额度"
        },
        year: {
            type: DataTypes.INTEGER,
            allowNull: true,
            comment: "年"
        },
        month: {
            type: DataTypes.INTEGER,
            allowNull: true,
            comment: "月"
        },
        disable: {
            type: DataTypes.INTEGER,
            allowNull: true,
            comment: "0是未禁用1是禁用"
        },
        is_del: {
            type: DataTypes.INTEGER,
            allowNull: true,
            defaultValue: 0,
            comment: "0是未删除1是已删除"
        },
    }, {
        tableName: 'keys',
        timestamps: false,
        indexes: [
            {
                name: "PRIMARY",
                unique: true,
                using: "BTREE",
                fields: [
                    { name: "id" },
                ]
            },
            {
                name: "idx_ting_png_key",
                using: "BTREE",
                fields: [
                    { name: "ting_png_key" },
                ]
            },
        ]
    });

    //创建一个api_response_test表对象，用于存储接口响应测试数据
    apiResponseTest = this.sequelize.define('api_response_test', {
        id: {
            autoIncrement: true,
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true
        },
        interval_time: {
            type: DataTypes.INTEGER,
            allowNull: false,
            unique: true,
            comment: "间隔时间（秒）"
        },
        response_time: {
            type: DataTypes.FLOAT,
            allowNull: false,
            comment: "响应时间（毫秒）"
        }
    }, {
        tableName: 'api_response_test',
        timestamps: false,
        indexes: [
            {
                name: "PRIMARY",
                unique: true,
                using: "BTREE",
                fields: [
                    { name: "id" },
                ]
            },
            {
                name: "idx_interval_time",
                unique: true,
                using: "BTREE",
                fields: [
                    { name: "interval_time" },
                ]
            },
        ]
    });

}

const database = new Database();

// 使用 alter: true 可以添加和修改字段，但为了确保索引被正确创建，也需要启用索引同步
database.sequelize.sync({
    alter: false,
    logging: false
});

export default database;